<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String serverPath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(date);
request.setAttribute("dateString", dateString);
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>
<%
   if("0".equals(request.getParameter("operateType"))){
%>
编辑报告
<%
   }else{
%>
查看报告
<%
   }
%>
</title>
<%@ include file="../jslib/jquerylib.jsp" %>
	<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <%
    request.setAttribute("pageContext",pageContext); 
    %>
    <script  type="text/javascript" charset="gbk">
        // 初始化控件
        var TANGER_OCX;
        var ids='';
        var filepath='';
        var jsonarrayBd; //流程中的数据
        var shiyanjielun =''; //试验结论
        var dianziqianmType =""    //电子签名类型
        var ysjlFilemodeId = '';//原始记录模版ID
        var values ;
        var sffz = '';
        var operateType = <%=request.getParameter("operateType")%>;//0:修改 1：查看  2：报告修改流程 修改报告页面中点击【修改报告】 3：批准报告中点击【批准报告】
        var stateid = '<%=request.getParameter("stateid")%>';
        var re_xinbgno = <%=request.getParameter("re_xinbgno")%> ; //新报告识别号
        var reportShb ='';     
        var attachmentId ='';  
        var imageReturnvalue ='';
        var jsonarrayQz;//电子签章
        function pageload() {
            TANGER_OCX = document.all("TANGER_OCX");
            
            if(operateType==0||operateType==1||operateType==3){
               $.get("<%=request.getContextPath() %>/codeReviewActionNew!getReportfilename.action?type=<%=request.getParameter("type")%>&ids=<%=request.getParameter("ids")%>&taskid=<%=request.getParameter("taskId")%>&childFlowid=<%=request.getParameter("childFlowid") %>&operateType="+operateType+"&mainId=<%=request.getParameter("mainid")%>",function(data){
                values = eval('(' + data + ')');
                reportShb = values.reportShb;
                jsonarrayBd = values.jsonarrayBd;
                attachmentId = values.attachmentId;
                jsonarrayQz = values.jsonarrayQz;
                if(values.reportName!= null && values.reportName!=""){
	               $("#filename").val(values.reportName);
	               $("#filemodeId").val(values.reportId);
                }
              
                if(values.reportPath!= null && values.reportPath!=""){
                    filepath =values.reportPath ;
              		TANGER_OCX.OpenFromURL("<%=request.getContextPath()%>/codeReviewActionNew!getTaskFileDownloadYsjlHgz.action?allpath="+values.reportPath );
              	}
				
				//设置新的报告识别号
				if(operateType=='0'){
				  setvalue('l识别号',re_xinbgno);
				}
             
			 })
            }
            else if(operateType==2){
               TANGER_OCX.OpenFromURL("<%=request.getContextPath()%>/codeReviewActionNew!getTaskFileDownloadYsjlHgz.action?allpath=<%=request.getParameter("filepath")%>");
            }
            
			TANGER_OCX.FileNew = false;//设置新建菜单
			TANGER_OCX.FileOpen = false; //设置打开菜单
			TANGER_OCX.FileClose = false; //设置关闭菜单
			TANGER_OCX.FileSave = false; //设置保存菜单
			TANGER_OCX.FileSaveAs = true; //设置另存菜单
			TANGER_OCX.FilePrint = true; //设置打印菜单
			TANGER_OCX.FilePrintPreview = true; //设置打印预览菜单
			TANGER_OCX.FilePageSetup = true; //设置打印设置菜单
			TANGER_OCX.FileProperties = true; //设置属性菜单
            
        }
        
    
		function unloadFile(){
			TANGER_OCX.Close();
		}
		window.onbeforeunload = function() {
		    var stateid = '<%=request.getParameter("stateid")%>';
		    if(document.body.clientWidth-event.clientX< 170&&event.clientY< 0||event.altKey) {
		        if($("#filename").val()!=''&&stateid=='proc7'){ 
		　　       return "当前报告还未保存,是否关闭?"; 
	            }
		    }
		}
	 
       function SaveDoc() {
            
            var msg = '';
            if(operateType=='3'){
               msg = encodeURIComponent(filepath);
            }
            var imageId ='';
            var returnValue = TANGER_OCX.SaveToURL('<%=basePath%>workflow/saveHgz.jsp?flag=0&ids=<%=request.getParameter("ids")%>&taskid=<%=request.getParameter("taskId")%>&filepath='+msg+'&sfdianziqianm='+$("#sfdianziqianm").val()+"&filename="+encodeURIComponent(encodeURIComponent($("#filename").val()))+"&reportId="+$("#filemodeId").val()+"&attachmentId="+imageId+"&bgfileType=1&operateType"+operateType, "EDITFILE", "", "newdoc.docx", 0);
			           
            if ("failed" != returnValue&&returnValue!='') {
            		 window.returnValue = returnValue;     
			            alert("文件上传成功");
			            TANGER_OCX.Close();  
			            window.close();  
            } else {
                alert("文件上传失败,请重试!");
            }
        }

        function addBookmark(mark) {
            TANGER_OCX.ActiveDocument.Bookmarks.Add(mark);
        }
        
       
		function btnClose(){
		    var stateid = '<%=request.getParameter("stateid")%>';
		    /**
		    if($("#filename").val()!=''){ 
		      if(!confirm("当前报告还未保存,是否关闭?")){
		        return false;
		      }
		    }
		    **/
			TANGER_OCX.Close();
			window.close();
		}
		
		//往标签里写值
        function setvalue(key, value,flag) {
           try
           {
              var bkmkObj = TANGER_OCX.ActiveDocument.BookMarks(key);
               bkmkObj.Range.Text = value;
           }
           catch(err)
           {
              //在此处理错误
           }
        }
        
        //判断是否存在标签
        function CheckExists(key) {
           try
           {
              var bkmkObj = TANGER_OCX.ActiveDocument.BookMarks(key);
              return true;
           }
           catch(err)
           {
           		return false;
              //在此处理错误
           }
        }
        
        //往对应坐标里写值
        function setTableValue(tableindex, rowindex, columnindex, value) {
            try{
              var temp = TANGER_OCX.ActiveDocument;
              var tables = temp.Tables(tableindex);
              var rows = tables.rows(rowindex);
              if(value==''||value==null){
                 rows.cells(columnindex).Range.Text = "/";
              }else{
                 rows.cells(columnindex).Range.Text = value;
              }
            }catch(err){ }
        }
       
        
		  //选择模版
		function selectModel()
		{
		      var url = "<%=request.getContextPath()%>/workflow/selectReportFileMode.jsp?filemodeid="+ysjlFilemodeId+"&temp="+Math.round(Math.random()*10000);
	          var  array = window.showModalDialog(url,null,"dialogHeight:500px;dialogWidth:600px;scroll:no");
	          if(typeof(array) == "undefined"){
				return false;
			  }else{
			    array = eval('('+array +')');
			    $("#filename").val(array.name);
			    $("#filemodeId").val(array.id);
			    TANGER_OCX.OpenFromURL("<%=request.getContextPath()%>/codeReviewActionNew!getTaskFileDownloadYsjlHgz.action?path="+array.path+"&type=reportmb");
	            initReport();
	          } 
	   }	
	   function initReport(){
	      //表单基本信息带值
	      for(var i=0;i<jsonarrayBd.length;i++){
		     setvalue(jsonarrayBd[i].label,jsonarrayBd[i].labelValue);
          }
          //报告识别号
          setvalue('报告识别号',reportShb);
         
          //获取报告图片
          $.ajax({
				type: "POST",
				url :'<%=request.getContextPath() %>/codeReviewActionNew!getReportImagePath.action?id='+attachmentId,  
				dataType: "json",
				async: false,//取消异步提交
				success : function(data, textStatus) {
					if(data.ajaxResult=="true"){
					  var imagesize = data.imagesize;
					  var imagecount = data.imagecount;
					  var imagepath = data.imagepath;
					  var count = data.count;
					  imagepath = imagepath.replace(/&/g,"\\");
					  if(imagepath!=''){
					     var allImages = imagepath.split("@");
					     var rowcount = Math.ceil(parseInt(allImages.length)/imagecount);
					     InsetTable(rowcount,imagecount,allImages,imagesize) ;
					  }
					}
				}
			});
			
	   }
	    function dianziqianming(){
               var flag = "true";
               
               if(operateType=='1'){
                 TANGER_OCX.SetReadOnly(false); 
               }
          	    var biaoqian = "";
          	    var timebiaoqian ="";
                if(operateType=='0'){
                   biaoqian = '报告者';                //制作报告
                   timebiaoqian ='制作报告日期';
                }
               else if(operateType=='3'){
                   if(stateid=='pizhun'){
                      biaoqian = 'l批准';                //批准者 
                   }
                   else  if(stateid=='fafang'){
                      biaoqian = 'l签章';                //签章
                   }
                }
                /**
                else if(operateType=='1'){
                    if('<%=request.getParameter("stateid")%>'=='bgsh' || '<%=request.getParameter("stateid")%>'=='proc14'){
                       biaoqian = '审核者';             //审核报告
                       timebiaoqian ='审核日期';
                    } 
                    else  if('<%=request.getParameter("stateid")%>'=='pzbg' || '<%=request.getParameter("stateid")%>'=='proc15'){
                       biaoqian = '批准者';             //批准报告
                       timebiaoqian ='批准日期';
                    }
                }
                **/
                if(biaoqian!=''){
                    if(!CheckExists(biaoqian)){
                       alert(biaoqian+"没有书签,不能进行电子签名!");
                       return false;
                    }
                    if('${sessionScope.TDM_USER.signAddress}'!=''){
	                     var path = '${sessionScope.TDM_USER.signAddress}';
	                     setvalue(biaoqian,'',1);
	                     //将光标定位到书签上
	                     TANGER_OCX.ActiveDocument.Application.Selection.GoTo(-1,0,0,biaoqian);
	                     try{
	                       //在光标处插上图片
		           		    TANGER_OCX.AddPicFromURL("<%=request.getContextPath()%>/codeReviewActionNew!getKnowledgeFileDownload.action?allpath="+encodeURIComponent(encodeURIComponent(path)), true, 0, 0, 1, 20, 1); 
			             }catch(err){
			               setvalue(biaoqian,'${sessionScope.TDM_USER.username}',1);
			             }
			        }else{
			           setvalue(biaoqian,'',1);
		               setvalue(biaoqian,'${sessionScope.TDM_USER.username}',1);
		            }
		            $("#sfdianziqianm").val('0');
                }
              if(operateType=='1'){
                 TANGER_OCX.SetReadOnly(true); 
               }
          } 		
		function uploadimage(){
		   imageReturnvalue = window.showModalDialog('<%=request.getContextPath()%>/workflow/changguijiance_uploadImage.jsp?id='+attachmentId,null,"dialogWidth=600px;dialogHeight=500px;resizable=yes");
		   
		}
	function InsetTable(rowcount,columncount,allImages,imagesize) {
	    rowcount = parseInt(rowcount);
	    columncount = parseInt(columncount);
	    TANGER_OCX.ActiveDocument.Application.Selection.GoTo(-1,0,0,'图片位置');
		var a = TANGER_OCX.ActiveDocument.Application.Selection,
		b = a.tables.add(a.range, rowcount, columncount);
		b.style = "网格型";
	    for(var i=1;i<=rowcount;i++){
	       for ( j = 0; columncount>j; j++){alert
	          var path = allImages[i*(j+1)-1];
	       // TANGER_OCX.ActiveDocument.Application.Selection.GoTo(3, i, j, ""),
	 	      TANGER_OCX.AddPicFromURL("<%=request.getContextPath()%>/codeReviewActionNew!getKnowledgeFileDownload.action?allpath="+encodeURIComponent(encodeURIComponent(path)), true, 0, 0, 1, 20, 1); 
		   }
	    }
	}
	
	//报告签章
	function qianzhang(){
	     var SYS_FILEPATH = '${sessionScope.SYS_FILEPATH}';
	     TANGER_OCX.SetReadOnly(false); 
	     $("input[name='qianzhangname']:checkbox").each(function(){ 
            if($(this).attr("checked")){
               var values = $(this).val().split("@");
               var name = values[0];
               var path = SYS_FILEPATH+'/'+values[1];
               var i = 1;
	           while (i<10) {
	             var biaoqian = name+i;
                 if (checkexist(biaoqian)) {
                     TANGER_OCX.ActiveDocument.Application.Selection.GoTo(-1,0,0,biaoqian);
                     try{
                       //在光标处插上图片
	           		    TANGER_OCX.AddPicFromURL("<%=request.getContextPath()%>/codeReviewActionNew!getKnowledgeFileDownload.action?allpath="+encodeURIComponent(encodeURIComponent(path)), true, 0, 0, 1, 20, 1); 
		             }catch(err){
		              
		             }
                 }
	              i=i+1;
	           }
            }
            TANGER_OCX.SetReadOnly(true); 
           SaveDoc();
        });
	}
		   // 判断标签是否存在
	   function checkexist(key) {
            try{
                var temp = TANGER_OCX.ActiveDocument.BookMarks(key);
                return true;
            }
            catch(err) {
                return false;
            }
        }
    </script>
    <style type="text/css">
   		html{
		    overflow-x: hidden;
			overflow-y: hidden;
		}  		
   </style>
</head>
<body onload="pageload()" onunload="unloadFile()">

	<table>
		<tr>
			<td style="padding-left:20px;">
			<% 
			   if("0".equals(request.getParameter("operateType"))||"3".equals(request.getParameter("operateType"))){
			%>
			 报告名称：<input type="text" id="filename" name="filename" value="" style="width:200px" readonly="readonly"/>
			<input type="button"  value="确定" onclick="SaveDoc()"/>&nbsp;&nbsp;
			<input type="button"  value="电子签名" onclick="dianziqianming()"/>&nbsp;&nbsp;
			<%
			}
			%>
			
		    <input type="button"  value="关闭" onclick="btnClose()"/>
			</td>
			<input type="hidden" id="sfdianziqianm" name="sfdianziqianm" value=""/>
			<input type="hidden" id="filemodeId" name="filemodeId" value=""/>
		</tr>
	</table>
	<table>
		<tr>
			<td>
				<div style="width:1050px;height:650px">
    		        <object id="TANGER_OCX" classid="clsid:C9BC4DFF-4248-4a3c-8A49-63A7D317F404" 
	                codebase="<%=basePath %>OfficeControl.cab#version=5,0,1,8" width="100%" height="700">
	                <param name="MakerCaption" value="北京天健通泰科技有限公司">
	                <param name="MakerKey" value="458CA98356A147C9E15250F8AA371CEAD3DD141E">
	                <param name="ProductCaption" value="报告录入"> 
	                <param name="ProductKey" value="27C47CA09AFFF0E778E865FEC533CF879E8572B1">
	                <SPAN STYLE="color:red">该网页需要控件浏览.浏览器无法装载所需要的文档控件.请检查浏览器选项中的安全设置.</SPAN>
	                </object>
    			</div>	
			</td>
		</tr>
	</table>

</body>
</html>